package com.cyf.plc

import com.cyf.plc.bean.SystemStatus
import com.cyf.plc.bean.WeatherResponse
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Test


/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {

    @Test
    fun testHex() {
        //"ee002501680301"
        val intArray = arrayOf(238, 0, 37, 1, 104, 3, 1)
        var deviceId = ""
        for (i in 0..5){
            println(i)
        }
        for (i in intArray) {
            var hexString = Integer.toHexString(i)
            if (hexString.length == 1) {
                hexString = "0$hexString";
            }
            deviceId+=hexString
            println(hexString)
        }
        println(deviceId)
        println(-101/10f)
        for (i in 0..100){
            println((i.toFloat()/100).toString())
        }
    }

    @Test
    fun testCname() {
        var json =
            "var returnCitySN = {\"cip\": \"61.155.3.58\", \"cid\": \"320100\", \"cname\": \"江苏省南京市\"};"
        json = json.substring(json.indexOf("{"))
        println("json:$json")
        println("json2:" + JSONObject(json).toString())
        println(JSONObject(json).get("cname"))
    }

    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun test_weather() {
        val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory())
            .build()
            .adapter(WeatherResponse::class.java)
        val json =
            "{\"msg\":\"success\",\"code\":0,\"data\":{\"weather\":\"{\\\"now\\\":{\\\"hum\\\":\\\"54\\\",\\\"vis\\\":\\\"16\\\",\\\"pres\\\":\\\"1011\\\",\\\"pcpn\\\":\\\"0.0\\\",\\\"fl\\\":\\\"20\\\",\\\"wind_sc\\\":\\\"1\\\",\\\"wind_dir\\\":\\\"南风\\\",\\\"wind_spd\\\":\\\"2\\\",\\\"cloud\\\":\\\"91\\\",\\\"wind_deg\\\":\\\"189\\\",\\\"tmp\\\":\\\"20\\\",\\\"cond_txt\\\":\\\"多云\\\",\\\"cond_code\\\":\\\"101\\\"},\\\"update\\\":{\\\"loc\\\":\\\"2020-04-20 13:18\\\",\\\"utc\\\":\\\"2020-04-20 05:18\\\"},\\\"basic\\\":{\\\"admin_area\\\":\\\"江苏\\\",\\\"tz\\\":\\\"+8.00\\\",\\\"location\\\":\\\"南京\\\",\\\"lon\\\":\\\"118.76741028\\\",\\\"parent_city\\\":\\\"南京\\\",\\\"cnty\\\":\\\"中国\\\",\\\"lat\\\":\\\"32.04154587\\\",\\\"cid\\\":\\\"CN101190101\\\"},\\\"status\\\":\\\"ok\\\"}\",\"air\":\"{\\\"no2\\\":\\\"24\\\",\\\"pm25\\\":\\\"27\\\",\\\"o3\\\":\\\"138\\\",\\\"qlty\\\":\\\"优\\\",\\\"so2\\\":\\\"10\\\",\\\"pub_time\\\":\\\"2020-04-20 12:00\\\",\\\"aqi\\\":\\\"44\\\",\\\"pm10\\\":\\\"41\\\",\\\"main\\\":\\\"-\\\",\\\"co\\\":\\\"0.9\\\"}\"}}"
        println("WeatherResponse:" + moshi.fromJson(json))
    }

    @Test
    fun testMoshi_json() {
        val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory())
            .build()
            .adapter(SystemStatus::class.java)
    }
}
